package tools.math;

/**
 * Extra math tools
 * 
 * @author Bart Moens
 *         Pieter Backaert
 */
public class MathTools {

	   public static double log2(double d) {
		      return Math.log(d)/Math.log(2.0);
		   }
	   

	   public static int log2ceil(double d) {
		      return (int)Math.ceil(Math.log(d)/Math.log(2.0));
		   }

       public static double power2dBM(double power){
            return (10* Math.log10(power/ 0.001));
       }

       public static double dBd2dBi(double dBd){
           return dBd+2.15;
       }

       public static double dBi2dBd(double dBi){
           return dBi-2.15;
       }

       public static double watt2dB(double watt){
           return (10* Math.log10(watt/1));
       }

       public static double dBm2mWatt(double dBm){
           return  Math.pow(10, (dBm/10) );//(10* Math.log10(watt/1));
       }
       public static double dBm2Watt(double dBm){
           return  Math.pow(10, (dBm/10) ) /1000;
       }
       

       public static double distanceBetween2Points(double x1 , double y1 , double x2, double y2){
           return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
       }

//       //WRONG
//       public static double calculateEfield(double power, double gain,  double distance) {
//           return Math.sqrt(30 * (power*gain)) / distance;
//
//           //return 0;
//       }
//
//       //WRONG
//       public static double calculateEfieldAdvanced(double power, double distance) {
//            return Math.sqrt((377*power)/(4*Math.PI*distance*distance)) * 1000000;
//       }


       // in microV/m
       public static double calculate_E_from_PathLoss(double EIRP , double PL , double freq){
           double ERP = EIRP - 2.15;
           double ERP_Watt = Math.pow(10, (ERP/10))/1000;

           double E_1kw = 139 - PL + 20 * Math.log10(freq/1000000);

           double EUV_1kW_lin = Math.pow(10, (E_1kw/20) );
           double EV_1kW_lin = EUV_1kW_lin * Math.pow(10 , -6); //E in [V/m]

           double E_lin = Math.sqrt(  ( Math.pow(EV_1kW_lin, 2) * ERP_Watt ) / 1000 );

           double E = 20 * Math.log10(E_lin * 1000000); //[dBµV/m]

           return E;
       }

       public static double microVperM2VperM(double input){
           return Math.pow(10, (input/20) )/1000000;
       }

       // in V/m
       public static double calculate_simple_E_from_PathLoss(double EIRP , double PL , double freq){
           double E = Math.pow(10, (-PL + EIRP - 43.15 + 20 * Math.log10(freq/1000000))/20 );
           return E;
       }
}
